package com.oujiong.redisson.strategy.impl;

import org.apache.commons.lang3.StringUtils;
import org.redisson.config.Config;

import com.oujiong.redisson.config.RedissonConfig;
import com.oujiong.redisson.constant.GlobalConstant;
import com.oujiong.redisson.strategy.RedissonConfigService;

import lombok.extern.slf4j.Slf4j;

/**
 * 单机部署Redisson配置
 */
@Slf4j
public class StandaloneConfigImpl implements RedissonConfigService {
	/**
	 * 根据不同的Redis配置策略创建对应的Config
	 *
	 * @param redissonProperties
	 *            redisson配置
	 * @return Config
	 */
	@Override
	public Config createRedissonConfig(final RedissonConfig redissonProperties) {
		final Config config = new Config();
		try {
			final String address = redissonProperties.getAddress();
			final String password = redissonProperties.getPassword();
			final int database = redissonProperties.getDatabase();
			final String redisAddr = GlobalConstant.REDIS_CONNECTION_PREFIX.getConstant_value() + address;
			config.useSingleServer().setAddress(redisAddr);
			config.useSingleServer().setDatabase(database);
			//密码可以为空
			if (StringUtils.isNotBlank(password)) {
				config.useSingleServer().setPassword(password);
			}
			StandaloneConfigImpl.log.info("初始化[单机部署]方式Config,redisAddress:" + address);
		} catch (final Exception e) {
			StandaloneConfigImpl.log.error("单机部署 Redisson init error", e);
		}
		return config;
	}
}
